package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.CostNameBean;
import com.koron.css2.baseConfig.bean.InvoiceTypeBean;
import com.koron.css2.baseConfig.mapper.CostInvoiceMapper;
import com.koron.css2.baseConfig.mapper.CostMapper;
import com.koron.css2.baseConfig.mapper.InvoiceTypeMapper;
import com.koron.css2.baseConfig.vo.CostInvoiceVO;
import com.koron.css2.baseConfig.vo.InvoiceTypeVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
/**
 * 费用票据关系-列表
 * @author 许海宜
 */
public class CostInvoiceList implements ServerInterface {
	private static Logger logger = LogManager.getLogger(CostInvoiceList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			//获取费用类型
			CostMapper costMapper = factory.getMapper(CostMapper.class);
			List<CostNameBean> costs = costMapper.selectCostNameList();
			//获取票据类型
			InvoiceTypeMapper invoiceTypeMapper = factory.getMapper(InvoiceTypeMapper.class);
			List<InvoiceTypeBean> invoiceTypes = invoiceTypeMapper.select();
			//根据费用类型查询
			List<CostInvoiceVO> result = new ArrayList<CostInvoiceVO>();
			CostInvoiceMapper mapper = factory.getMapper(CostInvoiceMapper.class);
			
			for (CostNameBean cost : costs) {
				List<InvoiceTypeVO> invoiceTypeList = new ArrayList<InvoiceTypeVO>();
				CostInvoiceVO ci = new CostInvoiceVO();
				for (InvoiceTypeBean invoiceType : invoiceTypes) {
					InvoiceTypeVO ivo = new InvoiceTypeVO();
					ivo.setInvoiceName(invoiceType.getInvoiceName());
					ivo.setCostInvoiceList(mapper.select(cost.getId(), invoiceType.getId()));
					invoiceTypeList.add(ivo);
				}
				ci.setCostName(cost.getName());
				ci.setInvoiceTypeList(invoiceTypeList);
				result.add(ci);
			}
			info.setCode(Constant.MESSAGE_INT_SUCCESS);
			info.setDescription("success");
			info.setData(result);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("操作失败");
			logger.error("操作失败", e);
		}
		return info;
	}

}
